<!DOCTYPE html><html lang="en"><head>
    <meta charset="utf-8">
    <title>How to create VR content</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:site" content="@threejs">
    <meta name="twitter:title" content="Three.js – How to create VR content">
    <meta property="og:image" content="https://threejs.org/files/share.png">
    <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
    <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">

    <link rel="stylesheet" href="../resources/lesson.css">
    <link rel="stylesheet" href="../resources/lang.css">
<script type="importmap">
{
  "imports": {
    "three": "../../build/three.module.js"
  }
}
</script>
  </head>
  <body>
    <div class="container">
      <div class="lesson-title">
        <h1>How to create VR content</h1>
      </div>
      <div class="lesson">
        <div class="lesson-main">
          
          <p>
            This guide provides a brief overview of the basic components of a web-based VR application
            made with three.js.
          </p>
        
          <h2>Workflow</h2>
        
          <p>
            First, you have to include [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/webxr/VRButton.js VRButton.js]
            into your project.
          </p>
        
<pre class="prettyprint notranslate lang-js" translate="no">
import { VRButton } from 'three/addons/webxr/VRButton.js';
</pre>
        
          <p>
            *VRButton.createButton()* does two important things: It creates a button which indicates
            VR compatibility. Besides, it initiates a VR session if the user activates the button. The only thing you have
            to do is to add the following line of code to your app.
          </p>
        
<pre class="prettyprint notranslate lang-js" translate="no">
document.body.appendChild( VRButton.createButton( renderer ) );
</pre>
        
          <p>
            Next, you have to tell your instance of `WebGLRenderer` to enable XR rendering.
          </p>
        
<pre class="prettyprint notranslate lang-js" translate="no">
renderer.xr.enabled = true;
</pre>
        
          <p>
            Finally, you have to adjust your animation loop since we can't use our well known
            *window.requestAnimationFrame()* function. For VR projects we use `renderer.setAnimationLoop()`.
            The minimal code looks like this:
          </p>
        
<pre class="prettyprint notranslate lang-js" translate="no">
renderer.setAnimationLoop( function () {

  renderer.render( scene, camera );

} );
</pre>
        
          <h2>Next Steps</h2>
        
          <p>
            Have a look at one of the official WebVR examples to see this workflow in action.<br /><br />
        
            [example:webxr_xr_ballshooter WebXR / XR / ballshooter]<br />
            [example:webxr_xr_cubes WebXR / XR / cubes]<br />
            [example:webxr_xr_dragging WebXR / XR / dragging]<br />
            [example:webxr_xr_paint WebXR / XR / paint]<br />
            [example:webxr_xr_sculpt WebXR / XR / sculpt]<br />
            [example:webxr_vr_panorama_depth WebXR / VR / panorama_depth]<br />
            [example:webxr_vr_panorama WebXR / VR / panorama]<br />
            [example:webxr_vr_rollercoaster WebXR / VR / rollercoaster]<br />
            [example:webxr_vr_sandbox WebXR / VR / sandbox]<br />
            [example:webxr_vr_video WebXR / VR / video]
          </p>

        </div>
      </div>
    </div>

  <script src="../resources/prettify.js"></script>
  <script src="../resources/lesson.js"></script>




</body></html>
